package paper

import (
	"data/config"
	"data/view/common"
	"data/view/question"
	"public/mysqldb"
	"strconv"
	"strings"

	"github.com/jinzhu/gorm"
)

/*
	获取试卷题目
*/
func GetPaperQuestions(str string) (qs []string, sum float64) {
	tmp := strings.Split(str, common.SPLIT)
	for _, v := range tmp {
		ss := strings.Split(v, ":")
		qs = append(qs, ss[0])
		score, _ := strconv.ParseFloat(ss[1], 64)
		sum += score
	}
	return
}

//获取试卷每题分值
func GetPaperQsScore(paper_no string) (score_sum float64, score_map map[string]float64) {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	score_map = make(map[string]float64)
	var paper Examination_tbl
	orm.Where("pid = ?", paper_no).Find(&paper)
	if paper.Id > 0 {
		tmp := strings.Split(paper.Questions, common.SPLIT)
		for _, v := range tmp {
			ss := strings.Split(v, ":")
			score, _ := strconv.ParseFloat(ss[1], 64)
			score_map[ss[0]] = score
			score_sum += score
		}
	}
	return
}

//获取试卷全部题目
func getMultiQuestionByPaper(paper_no string, questionArray []string, orm *gorm.DB) (result []question.Question_return) {
	// questionArray, _ := GetPaperQuestions(questions)
	_, score_m := GetPaperQsScore(paper_no)
	if len(questionArray) > 0 {
		for _, v := range questionArray {
			var info question.Question_tbl
			orm.Table("question_tbl").Where("quest_no = ?", v).Find(&info)
			if info.Id > 0 {
				var tmp question.Question_return
				tmp.Answer = strings.Split(info.Answer, common.SPLIT)
				tmp.Attach = info.Attach
				tmp.Bundle = info.Bundle
				tmp.Bundle_t = common.QuestionTypeEnMap[info.Bundle]
				tmp.Content = info.Content
				tmp.Full_score = score_m[v]
				tmp.Is_vaild = info.Is_vaild
				tmp.Limit_time = info.Limit_time
				tmp.Operator = info.Operator
				tmp.Options = strings.Split(info.Option, common.SPLIT)
				tmp.Picture = strings.Split(info.Picture, common.SPLIT)
				tmp.Pid = info.Pid
				tmp.Quest_no = info.Quest_no
				tmp.Title = info.Title
				tmp.Topic = info.Topic
				tmp.Difficulty = info.Difficulty
				result = append(result, tmp)
			}
		}
	}
	return
}

//判断多选题得分
func judgeMchoiceScore(content, answer []string, full_score float64) (score float64, i int) {
	for _, v := range content {
		falg := false
		for _, j := range answer {
			if v == j {
				falg = true
			}
		}
		if !falg {
			return
		}
	}
	//至少有对的
	for _, v := range content {
		for _, j := range answer {
			if v == j {
				i++
			}
		}
	}
	score = full_score * float64(i) / float64(len(answer))
	return
}

//获取试卷应考考生
func GetPaperAnswerer(paper_no string) (answerers []PaperAnswerer, score PaperScoreMinandMax) {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	orm.Table("exam_detail_tbl").Select("answerer,SUM(answer_time),SUM(score) as answerer_score").Where("paper_no = ?", paper_no).
		Group("answerer").Find(&answerers)

	orm.Raw("select MIN(score) as min_score,Max(score) as max_score from (select SUM(score) as score from exam_detail_tbl where paper_no = ? GROUP BY answerer) as answerers",
		paper_no).Scan(&score)

	return
}
